//https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/
package codeRandomThoughts.Test714买卖股票的最佳时机含手续费;

/**
 * 贪心思路
 * 真的不好理解 我是看不懂
 */
public class Solution2 {
    public int maxProfit(int[] prices, int fee) {
        int buy = prices[0] + fee;//购买的时候加入手续费

        int sum = 0;

        for (int price : prices) {
            //如果当前购入的价格比buy更低
            if (price + fee < buy) {
                //相当于前一天卖出了,今天要重新记录最小价格了
                buy = price + fee;
            }
            //如果当前价格比购入价格高
            else if (price > buy) {
                //其实不是真正地卖出,相当于持有股票
                sum += price - buy;
                buy = price;
            }
        }
        return sum;
    }
}
